home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / multi_c.exe / DEMO.CPP < prev    next >
C/C++ Source or Header  |  1992-11-08  |  12KB  |  610 lines

  1. /**********************************************************************
  2.  
  3.    = = = A General Demo of the MultiApp Core V1.0 = = =
  4.  
  5.  
  6.    Copyright 1992 by Jeff Heaton, MicroGenesis Software
  7.  
  8. Compilers Tested On:
  9.  
  10.  Borland C++ V3.0     - IBM DOS/WINDOWS
  11.  Turbo C++ V2.0      - IBM DOS/WINDOWS
  12.  Symantec Think C 5.0(with objects) - MAC System 6&7
  13.  
  14.  This demo shows some of the major features of the MultiApp core.
  15. The four demos are: FILEIO, Atomic Structures, Communications and timing.
  16. Because MultiApp core has ABSOLUTELY NO user interface one had to be provided
  17. in this demo.  Unfortunately ANSI C/C++ does not have an advanced enough
  18. user interface for this demo, so some compiler specific commands had to be
  19. used.  These are masked with #ifdef's using the keywords of IBM and MAC.
  20. The keyword of msWINDOWS may also be used with the windows version of
  21. MULTI-APP core.
  22.  
  23. MicroGenesis Software
  24. P.O. Box 25534
  25. St. Louis, MO 63125
  26.  
  27. CIS: 76476,1701
  28.  
  29. VOICE: (314) 638-2506
  30. BBS  : (314) 638-1731
  31.  
  32.  
  33.  MUST COMPILE UNDER LARGE MEMORY MODEL IF ON DOS
  34.  
  35.  **********************************************************************/
  36.  
  37. // ANSI-C comapt includes
  38. #include <stdio.h>
  39. #include <ctype.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42.  
  43. #include "multi_c.h"
  44.  
  45. // Compiler specific includes
  46. #ifdef MAC
  47. #include <console.h>
  48. #endif
  49.  
  50. #ifdef IBM
  51. #include <conio.h>
  52. #include <alloc.h>
  53. #endif
  54.  
  55.  
  56. // Even the ANSI-C interface cannot provide a system-independant enough
  57. // interface for this demo.
  58. int waiting=-1;// Character that is waiting to be read
  59.  
  60.  
  61. int key_get(void)// Get a SINGLE key stroke
  62. {
  63. #ifdef IBM
  64.  return getch();
  65. #endif
  66. #ifdef MAC
  67.  
  68.  if(waiting>0)
  69.   {
  70.   char c=waiting;
  71.   waiting=-1;
  72.   return c;
  73.   }
  74.  return getchar();
  75. #endif
  76. }
  77.  
  78. int key_hit(void)// Has the user hit a key?
  79. {
  80. #ifdef IBM
  81.  return kbhit();
  82. #endif
  83. #ifdef MAC
  84.  if(waiting>0)
  85.   return 1;
  86.  
  87.  waiting=getchar();
  88.  if(waiting>0)
  89.   return 1;
  90.  else return 0;
  91. #endif
  92. }
  93.  
  94.  
  95. // This is called internally by MultiApp when ALP times out.
  96. void alp_failed(void)
  97. {
  98.  printf("\n\nLine noise too bad, ALP connection lost.\n\n");
  99. }
  100.  
  101.  
  102.  
  103.  
  104. /************************************************************************
  105.  
  106. Demo for file sharing
  107.  
  108.  
  109.  ************************************************************************/
  110. void file_demo()
  111. {
  112. FILEIO *fp;
  113.  
  114.  printf("\n\nFILEIO/File Sharing Demo\n\n");
  115.  fp=new FILEIO;
  116.  
  117.  // Open file for read/write, which allows no shared access
  118.  // If we cannot gain that access now, then open_file waits for such
  119.  // access to become available.
  120.  fp->open_file("","DEMO.TXT",'+',"TEXT","DEMO");
  121.  
  122.  // Dump the file to stdout
  123.  while( !fp->end() )
  124.   putchar(fp->get(NULL));
  125.  
  126.  printf("\n\nPress any key so that other tasks may access this file.");
  127.  key_get();
  128.  
  129.  // Release file
  130.  fp->close_file();
  131.  delete fp;
  132. }
  133.  
  134.  
  135.  
  136. /************************************************************************
  137.  
  138. Demo For communications
  139.  
  140.  
  141.  ************************************************************************/
  142.  
  143. // Strip is a useful function that will remove path information from a
  144. // file name.  This works for both Macintosh and IBM File formats.
  145. // Example for IBM:
  146. // IN - C:\WINDOWS\SYSTEM\WIN.INI
  147. //  OUT- WIN.INI
  148. //
  149. // Example for Macintosh:
  150. //
  151. //  IN - MacintoshHD:System:system
  152. // OUT - system
  153. void strip(char *str)
  154. {
  155. char *ptr=str,*s1,*s2;
  156.  
  157.  s1=strchr(str,':');
  158.  s2=strchr(str,'\\');
  159.  
  160.  while( (s1!=NULL) || (s2!=NULL) )
  161.   {
  162.   if(s1>s2)
  163.    ptr=s1+1;
  164.   else
  165.    ptr=s2+1;
  166.  
  167.   s1=strchr(ptr,':');
  168.   s2=strchr(ptr,'\\');
  169.   }
  170.  strcpy(str,ptr);
  171. }
  172.  
  173.  
  174.  
  175. // rec_file - This function is called whenever a ctrl-A character is
  176. // received over the ALP link.  ctrl-A is simply the value I selected for this
  177. // demo to start a file transfer-- it is not a multi-app standard.  After
  178. // the ctrl-a the name and size(separated by carriage returns) should follow.
  179. //
  180. // This file transfer is just a straight binary dump.  There is no block size
  181. // implied at this level.  All error detection/compression is handled by
  182. // the ALP object(or any ALP-compatible object, which could use for example
  183. // netbios/appletalk rather than serial).
  184. void rec_file(ALP *com)
  185. {
  186. char name[80],size[80];
  187. FILEIO *fp;
  188. long todo;
  189. char str[80];
  190. int k=0;
  191. float f;
  192.  
  193.  com->getstr(name,79);// Needed values from remote
  194.  com->getstr(size,79);
  195.  
  196.  printf("\n\nReceiving: %s(%s)\n",name,size);
  197.  fp=new FILEIO;
  198.  fp->open_file("",name,'w',"TEXT","ttxt");// Save as teach text(if mac)
  199.  strip(name);
  200.  todo=atol(size);
  201.  
  202.  while(todo>0)
  203.   {
  204.   k=0;
  205.  
  206.   while(com->hit() && (k<512) )
  207.    {
  208.    fp->put(com->get(NULL));// This statement writes out every byte
  209.    todo--;
  210.    k++;
  211.  
  212.    if(key_hit())// Check for a local abort
  213.     {
  214.     if(key_get()==ESC)
  215.      {
  216.      com->putstr("~~~~");
  217.      break;
  218.      }
  219.     }
  220.  
  221.    }
  222.  
  223.   printf("%s: %ld         \r",name,todo);// Status line
  224.   com->update();
  225.   }
  226.  fp->close_file();
  227.  delete fp;
  228. }
  229.  
  230.  
  231. void send_file(ALP *com)
  232. {
  233. long l;
  234. char size[80];
  235. BYTE ch;
  236. FILEIO *fp;
  237. char name[80];
  238.  
  239.  if(com->no_alp)// This transfer wont work so well without ALP
  240.   {
  241.   printf("\n\nYou must establish an ALP link to transfer.\n\n");
  242.   return;
  243.   }
  244.  
  245.  printf("\nSend what file: ");// Input filename
  246.  gets(name);
  247.  
  248.  // Warn Macintosh users
  249. #ifdef MAC
  250.  printf("\n\nThis demo will ONLY transfer the data fork of a file!\n");
  251.  printf("Transparent resource fork transfer is planed for the next version.\n");
  252. #endif
  253.  
  254.  fp=new FILEIO;
  255.  fp->open_file("",name,'r',"","");
  256.  l=fp->file_length();
  257.  if(l<=0)
  258.   {
  259.   printf("\nFile Not Found\n");
  260.   delete fp;
  261.   return;
  262.   }
  263.  
  264.  strip(name);
  265.  com->printf("%c%s\r%ld\r",1,name,l);// Prompt other side to begin rec.
  266.  com->update();// Clear out the ALP buffer
  267.  while(com->intrans)
  268.   com->update();
  269.  do
  270.   {
  271.  
  272.   // This is how to read DIRECTLY into ALP's outbound buffer.
  273.   // WARNING: Make SURE nothing is in transit before you do this.
  274.   //      It is accomplished here in the loop while loop below.
  275.  
  276.   fp->fread(com->str,1024);
  277.  
  278.   com->p=l;// determine length(and short length on last "block")
  279.   if(l>1024)
  280.    com->p=1024;
  281.   l-=com->p;
  282.  
  283.   com->update();// Cause packet to be sent
  284.  
  285.   while(com->intrans)
  286.    com->update();// Loop until its done with that packet(see WARNING)
  287.  
  288.   printf("%s:%ld        \r",name,l);// Display status
  289.  
  290.   if(com->hit())// Check for remote abort
  291.    {
  292.    com->get(&ch);
  293.    if(ch=='~')
  294.     {
  295.     com->update();
  296.     break;
  297.     }
  298.    }
  299.  
  300.   if(key_hit())// Check for a local abort
  301.    {
  302.    if(key_get()==ESC)
  303.     {
  304.     com->putstr("~~~~");
  305.     break;
  306.     }
  307.    }
  308.  
  309.   } while( (l>0) && (!com->hit()) );
  310.  delete fp;
  311. }
  312.  
  313.  
  314.  
  315. void comm_demo()
  316. {
  317. ALP *com;
  318. char str[80];
  319. int done=0;
  320.  
  321.  com=new ALP;
  322.  com->direct=1;
  323.  
  324.  printf("\n\nTTY Terminal\n\n");
  325.  printf("Press ESC to quit, ~ to establish ALP, \\ to send a file.\n");
  326.  printf("Use comport(1-2)> ");
  327.  
  328.  gets(str);
  329.  if( com->init(atoi(str)) )
  330.     {
  331.     printf("\n\nThat comport does not exist\n\n");
  332.     return;
  333.     }
  334.  
  335.  
  336.  printf("Baud rate(300-38400)> ");
  337.  gets(str);
  338.  com->baud(atol(str));
  339.  
  340. // Turn mac console back to single character terminal
  341. #ifdef MAC
  342.  csetmode(C_RAW, stdin);
  343. #endif
  344.  
  345.  com->putstr("ATZ{");
  346.  
  347.  while(!done)
  348.   {
  349.   int ch;
  350.  
  351.   if(key_hit())
  352.    {
  353.    ch=key_get();
  354.  
  355.    if(ch==ESC)
  356.     done++;
  357.    else
  358.    if(ch=='~')
  359.     {
  360.     if(com->check())
  361.      printf("\n\nALP CONNECT\n");
  362.     }
  363.    else
  364.     if(ch=='\\')
  365.      send_file(com);
  366.    else
  367.     com->put(ch);
  368.    }
  369.  
  370.   while( com->hit() )
  371.    {
  372.    char ch=com->get(NULL);
  373.  
  374.    if(ch==1)
  375.     rec_file(com);
  376.    else putchar(ch);
  377.    }
  378.  
  379.   com->update();
  380.   }
  381.  delete com;
  382.  
  383. // Turn mac console back to normal
  384. #ifdef MAC
  385.  csetmode(C_ECHO, stdin);
  386. #endif
  387. }
  388.  
  389. /************************************************************************
  390.  
  391. Demo For Atomic Data Structures
  392.  
  393.  
  394.  ************************************************************************/
  395.  
  396. // Declare a simple atomic class to hold a string
  397. class DEMO_ITEM:public ATOM
  398. {
  399.  public:
  400.  DEMO_ITEM();
  401.  ~DEMO_ITEM();
  402.  
  403.  void set(char *str);
  404.  char *value(voi